home *** CD-ROM | disk | FTP | other *** search
- /*
- * Extract.c
- *
- */
-
- #define PSTRING "PLiteral ( %d , %d )"
- #define CSTRING "CLiteral ( %d , %d )"
-
- /*
- * You'll have to change MPW_INPUT to 1 and re-compile if you
- * extract MPW C string literals
- */
- #define REZ_OUTPUT 1
- #define MPW_INPUT 0
- #define EMPTY_STRING_ON_EMPTY_LIST 0
-
- #define PRINT_SCAN 0
- #define PRINT_STORE 0
-
- #include "Error.h"
- #include "Extract.h"
-
- #include "stdio.h"
- #include "string.h"
-
-
- /*
- * UGLY but necessary
- */
-
- long pos ;
- long end ;
- Handle data ;
- Handle output ;
- short theID ;
- short theNumber ;
- Boolean gotOne ;
-
-
- #define CHAR(x) ((x)>=(end)?0:(*data)[x])
- #define POS CHAR(pos)
-
- #if PRINT_SCAN
- #define NEWPOS if(POS==13)putchar(10);else putchar(POS);fflush(stdout);pos++
- #else
- #define NEWPOS pos++
- #endif
-
-
- static char
- ReadChar ( void ) {
-
- char theChar = POS ;
-
- NEWPOS ;
- if ( theChar == '\\' ) {
- theChar = POS ;
- if ( theChar >= '0' && theChar <= '7' ) {
-
- int num = 0 ;
- int ch = 0 ;
-
- while ( num < 3 && ( theChar = POS ) >= '0' && theChar <= '7' ) {
- num ++ ;
- ch <<= 3 ;
- ch += ( theChar - '0' ) ;
- NEWPOS ;
- }
- theChar = ch ;
- } else {
- NEWPOS ;
- switch ( theChar ) {
- #if MPW_INPUT
- /*
- * MPW is way brain-damaged for its string constants
- */
- case 'n' :
- theChar = 13 ;
- break ;
- case 'r' :
- theChar = 10 ;
- break ;
- #else
- case 'n' :
- theChar = 10 ;
- break ;
- case 'r' :
- theChar = 13 ;
- break ;
- #endif
- case 'b' :
- theChar = 8 ;
- break ;
- case 't' :
- theChar = 9 ;
- break ;
- case 'f' :
- theChar = 12 ;
- break ;
- default :
- break ;
- }
- }
- }
- return theChar ;
- }
-
-
- static void
- SkipCharLiteral ( void ) {
-
- while ( pos < end ) {
- if ( POS == '\'' ) {
- NEWPOS ;
- return ;
- }
- ( void ) ReadChar ( ) ;
- }
- }
-
-
- static void
- SkipToEndOfLine ( void ) {
- while ( pos < end ) {
- if ( POS == '\r' || POS == '\n' ) {
- NEWPOS ;
- return ;
- }
- NEWPOS ;
- }
- }
-
-
- static void
- SkipCComment ( void ) {
- while ( pos < end ) {
- if ( POS == '*' ) {
- NEWPOS ;
- if ( POS == '/' ) {
- NEWPOS ;
- return ;
- }
- } else {
- NEWPOS ;
- }
- }
- }
-
-
- static void
- HaveString ( Handle string , Boolean pascalMode , long fromPos ) {
-
- char buf [ 300 ] ;
- char bb [ 30 ] ;
- char * fromC , * endC ;
- unsigned char cc ;
-
- buf [ 0 ] = 0 ;
- bb [ 0 ] = 0 ;
- HLock ( string ) ;
- fromC = * string ;
- endC = GetHandleSize ( string ) + fromC ;
- while ( fromC < endC ) {
- cc = ( unsigned char ) * ( fromC ++ ) ;
- switch ( cc ) {
- case 0 :
- case 1 :
- case 2 :
- case 3 :
- case 4 :
- case 5 :
- case 6 :
- case 7 :
- case 11 :
- case 14 :
- case 15 :
- case 16 :
- case 17 :
- case 18 :
- case 19 :
- case 20 :
- case 21 :
- case 22 :
- case 23 :
- case 24 :
- case 25 :
- case 26 :
- case 27 :
- case 28 :
- case 29 :
- case 30 :
- case 31 :
- sprintf ( bb , "\\%03o" , cc ) ;
- strcat ( buf , bb ) ;
- break ;
- case 8 :
- strcat ( buf , "\\b" ) ;
- break ;
- case 9 :
- strcat ( buf , "\\t" ) ;
- break ;
- #if REZ_OUTPUT
- /*
- * We change these around for output because Rez is brain-damaged
- */
- case 10 :
- strcat ( buf , "\\r" ) ;
- break ;
- case 13 :
- strcat ( buf , "\\n" ) ;
- break ;
- #else
- case 10 :
- strcat ( buf , "\\n" ) ;
- break ;
- case 13 :
- strcat ( buf , "\\r" ) ;
- break ;
- #endif
- case 12 :
- strcat ( buf , "\\f" ) ;
- break ;
- case '\"' :
- strcat ( buf , "\\\"" ) ;
- break ;
- case '\\' :
- strcat ( buf , "\\\\" ) ;
- break ;
- default :
- bb [ 0 ] = cc ;
- bb [ 1 ] = 0 ;
- strcat ( buf , bb ) ;
- break ;
- }
- }
- PtrAndHand ( "\t\t\"" , output , 3 ) ;
- PtrAndHand ( buf , output , strlen ( buf ) ) ;
- PtrAndHand ( "\",\r" , output , 3 ) ;
- Error ( MemError ( ) ) ;
- HUnlock ( string ) ;
- if ( pascalMode ) {
- sprintf ( buf , PSTRING , theID , theNumber ) ;
- } else {
- sprintf ( buf , CSTRING , theID , theNumber ) ;
- }
- theNumber ++ ;
- Munger ( data , fromPos , NULL , pos - fromPos , buf , strlen ( buf ) ) ;
- pos = fromPos + strlen ( buf ) ;
- end = GetHandleSize ( data ) ;
- }
-
-
- static void
- ReadStringLiteral ( void ) {
-
- Handle string = NewHandle ( 0L ) ;
- long thatPos = pos - 1 ;
- char theChar ;
- Boolean pascalMode = 0 ;
-
- if ( ! string ) {
- Error ( MemError ( ) ) ;
- }
- gotOne = 1 ;
- if ( POS == '\\' && ( CHAR ( pos + 1 ) == 'P' || CHAR ( pos + 1 ) == 'p' ) ) {
- pascalMode = 1 ;
- NEWPOS ; NEWPOS ;
- }
- while ( pos < end ) {
- if ( POS == '\"' ) {
- NEWPOS ;
- HaveString ( string , pascalMode , thatPos ) ;
- DisposeHandle ( string ) ;
- #if PRINT_STORE
- putchar ( "\r\n" ) ;
- #endif
- return ;
- }
- theChar = ReadChar ( ) ;
- #if PRINT_STORE
- putchar ( theChar ) ; fflush ( stdout ) ;
- #endif
- PtrAndHand ( & theChar , string , 1 ) ;
- }
- DebugStr ( "\POoof!" ) ;
- DisposeHandle ( string ) ;
- }
-
-
- static void
- ReadLine ( void ) {
- while ( pos < end ) {
- switch ( POS ) {
-
- case 0 :
- case '\r' :
- case '\n' :
- NEWPOS ;
- return ;
-
- case ' ' :
- case '\t' :
- NEWPOS ;
- break ;
-
- case '\'' :
- NEWPOS ;
- SkipCharLiteral ( ) ;
- break ;
-
- case '#' :
- NEWPOS ;
- SkipToEndOfLine ( ) ;
- break ;
-
- case '/' :
- NEWPOS ;
- if ( POS == '*' ) {
- NEWPOS ;
- SkipCComment ( ) ;
- } else if ( POS == '/' ) {
- SkipToEndOfLine ( ) ;
- }
- break ;
-
- case '\"' :
- NEWPOS ;
- ReadStringLiteral ( ) ;
- break ;
-
- default :
- NEWPOS ;
- break ;
- }
- }
- }
-
-
- pascal short
- Extract ( Handle inData , Handle outData , short idBase , short numberBase ) {
-
- char buf [ 256 ] ;
-
- data = inData ;
- output = outData ;
- pos = 0 ;
- end = GetHandleSize ( inData ) ;
- theID = idBase ;
- theNumber = numberBase ;
-
- sprintf ( buf , "resource 'STR#' (%d) {\r\t{\r" , idBase ) ;
- PtrAndHand ( buf , output , strlen ( buf ) ) ;
- gotOne = 0 ;
- while ( pos < end ) {
- ReadLine ( ) ;
- }
- #if EMPTY_STRING_ON_EMPTY_LIST
- if ( ! gotOne ) {
- PtrAndHand ( "\t\t\"\"\r" , output , 5 ) ;
- }
- #endif
- PtrAndHand ( "\t}\r};\r" , output , 6 ) ;
-
- sprintf ( buf , "\r/**ID:%04d*/\r/**NM:%04d*/\r" , idBase , theNumber - 1 ) ;
- PtrAndHand ( buf , data , strlen ( buf ) ) ;
-
- return theNumber ;
- }
-
-